﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Controls.Ribbon;
using Tanmia.Core;
using Tanmia.Controls;

namespace Tanmia.Windows.UserScenario
{
    /// <summary>
    /// Interaction logic for UserEdit.xaml
    /// </summary>
    public partial class UserEdit : RibbonWindow
    {
        UserViewModel UserVM = new UserViewModel();


        public UserEdit()
        {
            InitializeComponent();

            DataContext = UserVM;
        }


        private void ShowValidationTool(string error)
        {
            vatError.Label = error;
            vatError.Visibility = Visibility.Visible;
        }

        private bool Save()
        {
            UserVM.Entity.Validate();

            string err = UserVM.Error;

            if (txtPassword.Password != txtRPassword.Password)
                if (string.IsNullOrEmpty(err))
                    err += "كلمتي المرور المدخلتان غير متطابقتان الرجاء إعادة المحاولة.";
                else
                    err += "\r\nكلمتي المرور المدخلتان غير متطابقتان الرجاء إعادة المحاولة.";

            if (!string.IsNullOrEmpty(err))
            {
                ShowValidationTool(err);
                return false;
            }
            else
            {
                try
                {
                    UserVM.Entity.Image = imageT.ImageByte;

                    UserVM.Save();

                    if (vatError.Visibility == Visibility.Visible)
                        vatError.Visibility = Visibility.Collapsed;

                    return true;

                }
                catch (Exception ex)
                {
                    ShowValidationTool(ex.Message);
                    return false;
                }
            }
        }


        private void RibbonWindow_Loaded(object sender, RoutedEventArgs e)
        {
            dtpCreateDate.SelectedDate = DateTime.Now;

            cmbRecord.IsEnabled = false;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            Save();
        }

        private void btnSaveAndStart_Click(object sender, RoutedEventArgs e)
        {
            if (Save())
            {
                System.Windows.Forms.Application.Restart();
                System.Windows.Application.Current.Shutdown();
            }
        }

        private void btnSaveAndPrint_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnPerviewPrint_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnSetupPrint_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            UserVM.Entity = new User();
            dtpCreateDate.SelectedDate = DateTime.Now;

            BindingExpression w = contactD.GetBindingExpression(ContactDetailTool.ContactDetailsProperty);
            w.UpdateSource();
            w.UpdateTarget();
            w = imageT.GetBindingExpression(ImageTool.ImageByteProperty);
            w.UpdateSource();
            w.UpdateTarget();

            txtPassword.Password = "";
            txtRPassword.Password = "";

            if (vatError.Visibility == Visibility.Visible)
                vatError.Visibility = Visibility.Collapsed;
        }

        private void btnSaveAndClose_Click(object sender, RoutedEventArgs e)
        {
            if (Save())
                Close();
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void btnLast_Click(object sender, RoutedEventArgs e)
        {
            UserVM.Last();
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            UserVM.Next();
        }

        private void btnPrevious_Click(object sender, RoutedEventArgs e)
        {
            UserVM.Previous();
        }

        private void btnFirst_Click(object sender, RoutedEventArgs e)
        {
            UserVM.First();
        }

        private void btnOptions_Click(object sender, RoutedEventArgs e)
        {

        }

        private void txtNote_TextChanged(object sender, TextChangedEventArgs e)
        {
            object o = DataContext;
        }

        private void txtPassword_PasswordChanged(object sender, RoutedEventArgs e)
        {
            UserVM.Entity.Password = ((PasswordBox)sender).Password;
        }

        private void Label_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            object obj = UserVM.Entity.Image;
        }

    }
}
